home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_new_tempnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  1.8 KB  |  76 lines

  1. /* author:    Monty Walls
  2.  * written:    4/17/89
  3.  * Copyright:    Copyright (c) 1989 by Monty Walls.
  4.  *        Not derived from licensed software.
  5.  *
  6.  *        Permission to copy and/or distribute granted under the
  7.  *        following conditions:
  8.  *    
  9.  *        1). This notice must remain intact.
  10.  *        2). The author is not responsible for the consequences of use
  11.  *            this software, no matter how awful, even if they
  12.  *            arise from defects in it.
  13.  *        3). Altered version must not be represented as being the 
  14.  *            original software.
  15.  */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18.  
  19. #define MAXPREFIX    5
  20. #define TMPNAME        "tmp"
  21. #ifndef P_tmpdir
  22. #define P_tmpdir    "/tmp"
  23. #define L_tmpnam    14
  24. #endif
  25.  
  26. #ifndef __STDC__
  27. extern char *mktemp();
  28. extern char *strcat();
  29. extern char *strcpy();
  30. extern char *getenv();
  31. extern char *malloc();
  32. #endif
  33.  
  34. char * tempnam(dir, name)
  35. char *dir;
  36. char *name;
  37. {
  38.     char *buf, *tmpdir;
  39.     
  40.     /* 
  41.      * This is kind of like the chicken & the egg.
  42.      * Do we use the users preference or the programmers?
  43.      */
  44. #if 1        /* this seems to be implied in the man page */
  45.     if ((tmpdir = getenv("TMPDIR")) == (char *)NULL) {
  46.         if ((tmpdir = dir) == (char *)NULL)
  47.             tmpdir = P_tmpdir;
  48.     }
  49. #else
  50.     if ((tmpdir = dir) == (char *)NULL) {
  51.         if ((tmpdir = getenv("TMPDIR")) == (char *)NULL)
  52.             tmpdir = P_tmpdir;
  53.     }
  54. #endif
  55.     /* now lets check and see if we can work there */
  56.     if (access(tmpdir, R_OK+W_OK+X_OK) < 0)
  57.         return ((char *)NULL);
  58.             
  59.     if (name == (char *)NULL) 
  60.         name = TMPNAME;
  61.     else if (strlen(name) > MAXPREFIX)
  62.         name[5] = '\0';    /* this is according to SYS5 */
  63.     
  64.     /* the magic value 2 is for '\0' & '/' */
  65.     if ((buf = (char *)malloc(L_tmpnam + strlen(tmpdir) + 2)) == (char *)NULL)
  66.         return ((char *)NULL);
  67.         
  68.     strcpy(buf, tmpdir);
  69.     strcat(buf, "/");
  70.     strcat(buf, name);
  71.     strcat(buf, ".XXXXXX");
  72.     
  73.     /* pass our completed pattern to mktemp */
  74.     return (mktemp(buf));
  75. }
  76.